package com.yao.component.pictureselector;
/**
 * 
 * @author Aizaz AZ
 *
 */

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.yao.component.pictureselector.photoview.PhotoViewAttacher;
import com.yao.component.pictureselector.widget.AlertMsgDialog;

import java.util.List;

 public class BasePhotoPreviewActivity extends Activity implements OnPageChangeListener, OnClickListener {

	private ViewPager mViewPager;
	private RelativeLayout layoutTop,layoutBottom;
	private ImageView btnBack,select;
	private TextView select_count;
	protected List<String> photosPath;
	protected int current;
	private LinearLayout finish;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
		setContentView(R.layout.activity_photopreview);
		finish=(LinearLayout)findViewById(R.id.finish);
		finish.setOnClickListener(this);
		select_count= (TextView) findViewById(R.id.select_count);
		layoutTop = (RelativeLayout) findViewById(R.id.layout_top_app);
		layoutBottom=(RelativeLayout)findViewById(R.id.layout_bottom_app);
		btnBack = (ImageView) findViewById(R.id.btn_back_app);
		select= (ImageView) findViewById(R.id.select);
		mViewPager = (ViewPager) findViewById(R.id.vp_base_app);
		select.setOnClickListener(this);
		btnBack.setOnClickListener(this);
		mViewPager.addOnPageChangeListener(this);

		overridePendingTransition(R.anim.activity_alpha_action_in, 0); // 渐入效果
		initData();
	}
	public  void initData(){
		Bundle bundle=getIntent().getExtras();
		photosPath=bundle.getStringArrayList("paths");
		current=bundle.getInt("position", 0);
		updateSelectState();
		bindData();
	}
	/** 绑定数据，更新界面 */
	protected void bindData() {
		mViewPager.setAdapter(mPagerAdapter);
		mViewPager.setCurrentItem(current);

	}

	private PagerAdapter mPagerAdapter = new PagerAdapter() {

		@Override
		public int getCount() {
			if (photosPath == null) {
				return 0;
			} else {
				return photosPath.size();
			}
		}

		@Override
		public View instantiateItem(final ViewGroup container, final int position) {
////			PhotoPreview photoPreview = new PhotoPreview(getApplicationContext());
////			((ViewPager) container).addView(photoPreview);
////			photoPreview.loadImage("file://"+photosPath.get(position));
////			photoPreview.setOnClickListener(photoItemClickListener);
//
//			PhotoItemView photoPreview2 = new PhotoItemView(getApplicationContext());
//			((ViewPager) container).addView(photoPreview2);
//			photoPreview2.loadImage("file://"+photosPath.get(position));
////			photoPreview2.setOnClickListener(photoItemClickListener);
//
//			return photoPreview2;

			MyPhotoView photoView = new MyPhotoView(container.getContext());
			photoView.loadImage("file://"+photosPath.get(position));
			photoView.setOnViewTapListener(viewTapListener);
			// Now just add PhotoView to ViewPager and return it
			container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

			return photoView;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}

	};
	protected boolean isUp;

	@Override
	public void onClick(View v) {
		int id=v.getId();
		if (id==R.id.btn_back_app){
			if(!isUp){
				finish();
			}

		}else if(id== R.id.select){
			if (isUp) {
				if (PhotoBrowseActivity.mSelectedImage.contains(photosPath.get(current))) {
					PhotoBrowseActivity.mSelectedImage.remove(photosPath.get(current));
					Intent intent = new Intent();
					intent.setAction("com.yao.component.pictureselector.selectupdata");
					BasePhotoPreviewActivity.this.sendBroadcast(intent);
				} else {
					if (PhotoBrowseActivity.mSelectedImage.size() >= PhotoBrowseActivity.maxSelectCount) {
						AlertMsgDialog dialog = new AlertMsgDialog(BasePhotoPreviewActivity.this, "你最多只能选择" + PhotoBrowseActivity.maxSelectCount + "张照片", "确定", null);
						dialog.show();
					} else {
						PhotoBrowseActivity.mSelectedImage.add(photosPath.get(current));
						Intent intent = new Intent();
						intent.setAction("com.yao.component.pictureselector.selectupdata");
						BasePhotoPreviewActivity.this.sendBroadcast(intent);
					}

				}

				updateSelectState();
			}
		}else if(id==  R.id.finish){
			if(!isUp) {
				if (PhotoBrowseActivity.mSelectedImage.size() == 0) {
					PhotoBrowseActivity.mSelectedImage.add(photosPath.get(current));
					updateSelectState();
				}

				Intent intent = new Intent();
				setResult(RESULT_OK, intent);
				BasePhotoPreviewActivity.this.finish();
			}
		}
//		switch (v.getId()){
//
//			case R.id.btn_back_app:
//				finish();
//				break;
//			case R.id.select:
//				if (PhotoBrowseActivity.mSelectedVideo.contains(photosPath.get(current))){
//					PhotoBrowseActivity.mSelectedVideo.remove(photosPath.get(current));
//					Intent intent = new Intent();
//					intent.setAction("com.yao.component.pictureselector.selectupdata");
//					BasePhotoPreviewActivity.this.sendBroadcast(intent);
//				}else {
//					if (PhotoBrowseActivity.mSelectedVideo.size()>=PhotoBrowseActivity.maxSelectCount){
//						AlertMsgDialog dialog=new AlertMsgDialog(BasePhotoPreviewActivity.this,"你最多只能选择"+PhotoBrowseActivity.maxSelectCount+"张照片","确定",null);
//						dialog.show();
//					}else{
//						PhotoBrowseActivity.mSelectedVideo.add(photosPath.get(current));
//						Intent intent = new Intent();
//						intent.setAction("com.yao.component.pictureselector.selectupdata");
//						BasePhotoPreviewActivity.this.sendBroadcast(intent);
//					}
//
//				}
//
//				updateSelectState();
//				break;
//			case  R.id.finish:
//				if ( PhotoBrowseActivity.mSelectedVideo.size()==0){
//					PhotoBrowseActivity.mSelectedVideo.add(photosPath.get(current));
//					updateSelectState();
//				}
//
//				Intent intent=new Intent();
//				setResult(RESULT_OK,intent);
//				BasePhotoPreviewActivity.this.finish();
//				break;
//		}


	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	@Override
	public void onPageSelected(int arg0) {
		current = arg0;
		updateSelectState();
	}

	protected void updateSelectState() {
		if (PhotoBrowseActivity.mSelectedImage.contains(photosPath.get(current)))
		{
			select.setBackgroundResource(R.drawable.circular_check);
		}else {
			select.setBackgroundResource(R.drawable.circular_uncheck);
		}
		if (PhotoBrowseActivity.mSelectedImage.size()>0){
			select_count.setText(PhotoBrowseActivity.mSelectedImage.size()+"");
			select_count.setVisibility(View.VISIBLE);
		}else {
			select_count.setVisibility(View.GONE);
		}
	}


	private PhotoViewAttacher.OnViewTapListener viewTapListener=new PhotoViewAttacher.OnViewTapListener() {
		@Override
		public void onViewTap(View view, float x, float y) {
			if (!isUp) {
				new AnimationUtil(getApplicationContext(), R.anim.top_translate_up)
						.setInterpolator(new LinearInterpolator()).setFillAfter(true).startAnimation(layoutTop);
				new AnimationUtil(getApplicationContext(), R.anim.bottom_translate_down)
						.setInterpolator(new LinearInterpolator()).setFillAfter(true).startAnimation(layoutBottom);
				isUp = true;
			} else {
				new AnimationUtil(getApplicationContext(), R.anim.top_translate_down)
						.setInterpolator(new LinearInterpolator()).setFillAfter(true).startAnimation(layoutTop);
				new AnimationUtil(getApplicationContext(), R.anim.bottom_translate_up)
						.setInterpolator(new LinearInterpolator()).setFillAfter(true).startAnimation(layoutBottom);
				isUp = false;
			}
		}
	};
}
